/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.checker.type;


import java.util.Iterator;
import java.util.List;


/**
 * A Dex data type.
 */
public abstract class Type {

	
	/**
	 * The manager that created this type.
	 */
	final TypeManager manager;
	
	
	/**
	 * Constructs a new compound type.
	 * 
	 * @param manager the manager that created this type
	 */
	Type(TypeManager manager) {
		this.manager = manager;
	}
	
	
	/**
	 * Constructs a new simple type.
	 */
	Type() {
		this(null);
	}

	/**
	 * Returns true if this type is a compound type.
	 * 
	 * @return  true if this type is compound
	 */
	public abstract boolean isCompound();
	
	
	/**
	 * Returns true if this type is a local type.
	 * 
	 * @return  true if this type is local
	 */
	public abstract boolean isLocal();
	
	
	/**
	 * Returns true if this type can be assigned to the given formal type.
	 * 
	 * @param formal  the formal type to check against
	 * @return   true if this type can be assigned to that type, false otherwise
	 */
	public abstract boolean canAssignTo(Type formal);


	/**
	 * Returns true if this type can be cast to the given type.
	 * 
	 * @param other  the type to check against
	 * @return       true if this type can be cast to that type, false otherwise
	 */
	public boolean canCastTo(Type other) {
		return other.canAssignTo(this);
	}
	
	
	/**
	 * Returns true if the list of actual parameters can be assigned to
	 * the list of formal parameters.
	 * 
	 * @param actual  the list of actual parameters
	 * @param formal  the list of formal parameters
	 * @return  true  if the actual parameters can be sent to the formal parameters
	 */
	public static boolean canAssignTo(List<Type> actual, List<Type> formal) {
		if (actual.size() != formal.size()) {
			return false;
		}
		Iterator<Type> actualIter = actual.iterator();
		Iterator<Type> formalIter = formal.iterator();
		while (actualIter.hasNext()) {
			Type actualParam = actualIter.next();
			Type formalParam = formalIter.next();
			if (!actualParam.canAssignTo(formalParam)) {
				return false;
			}
		}
		return true;
	}

	
	
	public abstract String toString();

}
